﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.xinxi
{
    public partial class xinxi_comment_list : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;

        protected int status;
        protected string keywords = string.Empty;
        protected int type_id = 0;
        protected int xinxi_id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.status = DTRequest.GetQueryInt("status");
            this.keywords = DTRequest.GetQueryString("keywords");
            this.xinxi_id = DTRequest.GetQueryInt("xinxi_id");
            //this.type_id = DTRequest.GetQueryInt("type_id");

            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                //ChkAdminLevel("sys_channel_manage", DTEnums.ActionEnum.View.ToString()); //检查权限
                GetQuanzi();
                RptBind("id>0" + CombSqlTxt( xinxi_id, keywords), "id desc");
            }
        }

        #region 绑定类别=================================
        private void GetQuanzi()
        {
            //BLL.quanzi bll = new BLL.quanzi();
            //DataTable dt = bll.GetList(0, "", "sort_id asc, id desc").Tables[0];

            //this.ddlQuanziID.Items.Clear();
            //this.ddlQuanziID.Items.Add(new ListItem("所有圈子", ""));

            //foreach (DataRow dr in dt.Rows)
            //{
            //    string Id = dr["id"].ToString();
            //    string Title = dr["title"].ToString().Trim();
            //    this.ddlQuanziID.Items.Add(new ListItem(Title, Id));
            //}
        }
        #endregion

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
           
            txtKeywords.Text = this.keywords;
            //ddlQuanziID.SelectedValue = this.quanzi_id.ToString();
            BLL.xinxi_comment bll = new BLL.xinxi_comment();
            this.rptList.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("xinxi_comment_list.aspx", "xinxi_id={0}&keywords={1}&page={2}", this.xinxi_id.ToString(), this.keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt( int _quanzi_id, string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
          
            if (xinxi_id > 0)
            {
                strTemp.Append(" and article_id=" + _quanzi_id);
            }
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (remark like  '%" + _keywords + "%' )");
            }

            return strTemp.ToString();
        }
        #endregion

        #region 返回每页数量=============================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("gongying_page_size", "DTcmsPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("xinxi_comment_list.aspx", "keywords={0}",  txtKeywords.Text));
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("gongying_page_size", "DTcmsPage", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("xinxi_comment_list.aspx", "keywords={0}",  this.keywords));
        }


        //批量删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            //ChkAdminLevel("sys_channel_manage", DTEnums.ActionEnum.Delete.ToString());//检查权限
            int sucCount = 0;
            int errorCount = 0;
            BLL.xinxi_comment bll = new BLL.xinxi_comment();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (bll.Delete(id))
                    {
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }
            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！",
                Utils.CombUrlTxt("xinxi_comment_list.aspx", "keywords={0}", this.keywords), "parent.loadMenuTree");
        }

      
    }
}